import { Model, DataTypes, Sequelize } from 'sequelize'
import { sqliteSequelizeFactory } from '@/util/SqliteDbUtil'
import { getOsHomeDir } from '@/util/FileUtil'
import { join } from 'path'

// const projectHomeDir = await mkdirAsync(getOsHomeDir(), 'pan')
const projectHomeDir = join(getOsHomeDir(), 'pan')
const dbFilePath = join(projectHomeDir, 'test.db')

export function systemSqliteSequelizeFactory() {
  return sqliteSequelizeFactory(dbFilePath)
}

let sequelize: Sequelize
if (!sequelize) {
  sequelize = systemSqliteSequelizeFactory()
}

export interface UserInstance {
  id?: number
  /**
   * 邮箱
   */
  email: string
  name: string
  age?: number
  birthday: Date
  regDate?: Date
  remark?: string
}

export const UserDao = sequelize.define('User', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
  },
  email: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    defaultValue: '',
  },
  age: {
    type: DataTypes.INTEGER,
  },
  birthday: {
    type: DataTypes.DATE,
  },
  regDate: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
  },
  remark: {
    type: DataTypes.TEXT,
  },
})

export default sequelize
